<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
  <script src="../js/axios.js"></script>
  <!-- <script src="./my-axios.js"></script> -->
  <script>
    let loginData = {
      username: 'admin1',
      password: '123321'
    }
    // axios默认配置
    axios.defaults.baseURL = 'http://47.94.46.113:7788'
    // axios的请求拦截器
    axios.interceptors.request.use((config) => {
      // console.log(config);
      // 1.获取token
      let token = '123123'
      // 2.判断token设置条件
      if (config.url != '/user/login') {
        config.headers['Authorization'] = token
      }
      return config
    }, (error) => {
      return Promise.reject(error)
    })

    // axios的响应拦截器
    let interceptor = axios.interceptors.response.use((response) => {
      console.log(response);
      response = {
        ...response,
        data: response.data.data,
        statusText: response.data.message
      }
      return response
    }, (error) => {
      return Promise.reject(error)
    })

    // axios.post('/user/login', loginData).then(res => {
    //   console.log(res);
    // }).catch(error => {

    // })

    let instance1 = axios.create({
      baseURL: 'http://47.94.46.112:7788',
      headers: {
        'Authorization': 'token'
      }
    })
    let instance2 = axios.create({
      baseURL: 'http://47.94.46.112:8888',
      headers: {
        'Contene-Type': 'application/x-www-form-urlencoded'
      }
    })

    console.log('-----', instance1);
    instance1({
      method: 'post',
      url: '/user/login',
      data: loginData
    }).then(res => {
      console.log(res);
    })

    // setTimeout(() => {
    //   // 3秒中之后，将响应拦截器进行移除
    //   axios.interceptors.response.eject(interceptor)
    //   axios.post('/user/login', loginData).then(res => {
    //     console.log(res);
    //   })
    // }, 3000)
    // axios.get('/user/info')
  </script>
</head>

<body>

</body>

</html>